from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional, List

from pydantic import BaseModel

from models import CmsGaodeFence


class CmsGaodeTerminalVO(BaseModel):
    """
    高德终端对应pydantic模型
    """
    id: Optional[int]
    service_id: Optional[str]
    terminal_id: Optional[str]
    terminal_name: Optional[str]
    terminal_desc: Optional[str]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]

    class Config:
        orm_mode = True


class CmsGaodeFenceVO(BaseModel):
    """
    高德终端对应pydantic模型
    """
    id: Optional[str] = None
    service_id: Optional[str] = None
    fence_id: Optional[str] = None
    fence_type: Optional[str] = None
    fence_name: Optional[str] = None
    fence_desc: Optional[str] = None
    circle_center: Optional[str] = None
    circle_radius: Optional[str] = None
    polygon_points: Optional[str] = None
    polyline_points: Optional[str] = None
    polyline_bufferradius: Optional[int] = None
    district_adcode: Optional[str] = None
    state: Optional[int] = None
    create_time: Optional[datetime] = None
    create_by: Optional[str] = None
    update_time: Optional[datetime] = None
    update_by: Optional[str] = None
    cars: Optional[List[str]] = None  # 允许 cars 字段为空
    car_numbers: Optional[List[str]] = None  # 允许 cars 字段为空
    alarm_type: Optional[str] = None

    class Config:
        orm_mode = True


class CmsGaodeFenceResponse(BaseModel):
    data: List[CmsGaodeFenceVO]
    total: int


class TerminalPointQuery(BaseModel):
    terminal_id: str
    start_time: Optional[str] = None
    end_time: Optional[str] = None

    # 注意：这里不需要 Config 类或 orm_mode


class OrderQuery(BaseModel):
    order_id: str


class PageObjectResponse(BaseModel):
    """
            用户管理列表分页查询返回模型
            """
    rows: List = []
    page_num: int
    page_size: int
    total: int
    has_next: bool


class PaginationQuery(BaseModel):
    page_num: int
    page_size: int
    key_word: Optional[str] = None


class PaginationQueryForPage(BaseModel):
    page_num: int
    page_size: int
    car_number: Optional[str] = Field(None, title="车牌号")
    start_time: Optional[datetime] = Field(None, comment='开始时间')
    end_time: Optional[datetime] = Field(None, comment='结束时间')
    alarm_type: Optional[str] = Field(None, comment='告警类型')

class CarModelForAll(BaseModel):
    car_id: Optional[str] = None
    device_id: Optional[str] = None
    car_number: Optional[str] = None
    tid: Optional[str] = None
    fence_id: Optional[str] = None
    alarm_type: Optional[str] = None
    fence_name: Optional[str] = None
    polygon_points: Optional[str] = None
    state: Optional[str] = None
    order_id: Optional[str] = None
    use_car_type: Optional[str] = None
    driver_id: Optional[str] = None
    driver_name: Optional[str] = None
    start: Optional[str] = None
    destination: Optional[str] = None
    gaode_track_id: Optional[str] = None

class CarModelForAllTrack(BaseModel):

    car_id: Optional[str]
    device_id: Optional[str]
    car_number: Optional[str]
    lon: Optional[str]
    lat: Optional[str]
    height: Optional[int]
    speed: Optional[int]
    direction: Optional[int]
    execute_id: Optional[str]
    driver_id: Optional[str]
    driver_name: Optional[str]
    service_id: Optional[str]
    terminal_id: Optional[str]


class CarModelForPage(BaseModel):
    car_id: Optional[str] = Field(None, comment='车辆id(字符串)')
    device_id: Optional[str] = Field(None, comment='设备id(字符串)')
    car_number: Optional[str] = Field(None, comment='车牌号(字符串)')
    state: Optional[str] = Field(None, comment='车辆在线离线状态')
    order_id: Optional[str] = Field(None, comment='订单id(字符串)')
    use_car_type: Optional[str] = Field(None, comment='用车类型')
    driver_id: Optional[str] = Field(None, comment='驾驶员id(字符串)')
    driver_name: Optional[str] = Field(None, comment='驾驶员姓名')
    start: Optional[str] = Field(None, comment='开始位置')
    destination: Optional[str] = Field(None, comment='结束位置')
    distance: Optional[str] = Field(None, comment='距离 单位千米')
    lon: Optional[str] = Field(None, comment='经度')
    lat: Optional[str] = Field(None, comment='维度')
    time: Optional[str] = Field(None, comment='时间 单位小时')
    use_car_type_name: Optional[str] = Field(None, comment='用车类型名称')



    class Config:
        orm_mode = True


class CmsGaodeFenceTerminalVO(BaseModel):
    """
    高德围栏终端关系表对应pydantic模型
    """
    id: Optional[int]
    service_id: Optional[str]
    fence_id: Optional[str]
    terminal_id: Optional[str]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]
    tid: Optional[str]


class CmsGaodeServiceVO(BaseModel):
    id: Optional[int]
    service_id: Optional[str]
    service_name: Optional[str]
    service_desc: Optional[str]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]


class CmsGaodeTraceVO(BaseModel):
    id: Optional[int]
    service_id: Optional[str]
    terminal_id: Optional[str]
    trace_id: Optional[str]
    trace_name: Optional[str]
    distance: Optional[int]
    time: Optional[int]
    count: Optional[int]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]
    execute_id: Optional[str]


class CmsTerminalPointVO(BaseModel):
    id: Optional[int]
    terminal: Optional[str]
    lon: Optional[str]
    lat: Optional[str]
    height: Optional[int]
    speed: Optional[int]
    direction: Optional[int]
    up_time: Optional[datetime]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]

class CmsTerminalPointVO2(BaseModel):
    id: Optional[int]
    terminal: Optional[str]
    lon: Optional[str]
    lat: Optional[str]
    height: Optional[int]
    speed: Optional[int]
    direction: Optional[int]
    up_time: Optional[datetime]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]
    car_number: Optional[str]
    device_id: Optional[str]


class CmsAlertRuleVO(BaseModel):
    id: Optional[int]
    fence_id: Optional[str]
    type: Optional[str]
    car_id: Optional[str]
    car_number: Optional[str]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]


class CmsAlertMessageVO(BaseModel):
    id: Optional[int]
    fence_id: Optional[str]
    fence_name: Optional[str]
    car_id: Optional[str]
    car_number: Optional[str]
    alert_type: Optional[str]
    alert_msg: Optional[str]
    handle: Optional[str]
    handle_by: Optional[str]
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]
    location: Optional[str]

class CmsAlertMessageQuery(BaseModel):
    alert_type: Optional[str]
    car_number: Optional[str]
    begin_time: Optional[datetime]
    end_time: Optional[datetime]

class CmsAlertMessageQueryPage(CmsAlertMessageQuery):
    page_num: int
    page_size: int


class CmsCarTrackVO(BaseModel):
    # 不需要
    id: Optional[int]
    car_id: Optional[str]
    device_id: Optional[str]
    # 当前时间
    record_time: Optional[datetime]
    car_number: Optional[str]
    lon: Optional[str]
    lat: Optional[str]
    height: Optional[int]
    speed: Optional[int]
    direction: Optional[int]
    # 自定义
    track_id: Optional[str]
    execute_id: Optional[str]
    driver_id: Optional[str]
    driver_name: Optional[str]
    # 高德返回
    gaode_track_id: Optional[str]
    # 创建添加
    track_name: Optional[str]
    service_id: Optional[str]
    terminal_id: Optional[str]
    # 默认1
    state: Optional[int]
    create_time: Optional[datetime]
    create_by: Optional[str]
    update_time: Optional[datetime]
    update_by: Optional[str]


class CmsCarTrackPointVO(BaseModel):
    location: Optional[str]
    height: Optional[int]
    speed: Optional[int]
    direction: Optional[int]
    locatetime: Optional[str]

    def to_json(self):
        # 定义一个方法将对象转换为字典
        return {
            'location': self.location,
            'height': self.height,
            'speed': self.speed,
            'direction': self.direction,
            'locatetime': self.locatetime
        }


class CmsCarVO(BaseModel):
    car_id: Optional[str]
    device_id: Optional[str]
    car_number: Optional[str]
    order_id: Optional[str]
    use_car_type: Optional[str]
    driver_name: Optional[str]
    state: Optional[int]


class CarMinitorObjectForQuery(BaseModel):
    state: Optional[str] = Field(None, comment='车辆在线离线状态 online offline')
    car_number: Optional[str] = Field(None, comment='车牌号(字符串)')
    car_number_list: Optional[List[str]] = Field(None, comment='车牌号(字符串集合)')


class CarMinitorObject(CarMinitorObjectForQuery):
    """
    岗位管理分页查询模型
    """
    page_num: int
    page_size: int


